//
//  ViewBaseController.m
//  projectBase
//
//  Created by zhuangzhh on 14-11-15.
//  Copyright (c) 2014年 zhuangzhh. All rights reserved.
//

#import "ViewBaseController.h"
#import "SizeHeader.h"

#define ToolbarCancelDoneTag  50000
#define NOCONTENTVIEWTAG      50001


#define currentMonth [currentMonthString integerValue]

@interface ViewBaseController ()

@end

@implementation ViewBaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    snapArray = [NSMutableArray array];
    
    
//    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
//    statusBarView.backgroundColor=[UIColor blackColor];
//    [self.view addSubview:statusBarView];
//    
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

}


-(void)showLoadView
{
    if(_comLoadView != nil)
    {
        [_comLoadView removeFromSuperview];
        _comLoadView = nil;
    }
    
    CGRect rect = CGRectMake(0, TopBarHeight, ScreenWidth,(ScreenHeight - TopAndSystemHeight - BottomHeight));
    NSString* info = @"载入数据中...";
    _comLoadView = [[CommonLoadingView alloc] initWithInfo:rect info:info];
    [self.view addSubview:_comLoadView];
}

-(void)closeLoadView
{
    if(_comLoadView != nil)
    {
        [_comLoadView removeFromSuperview];
        _comLoadView = nil;
    }
}

-(void)showLoadView:(CGRect)rect backColor:(UIColor*)backColor
{
    [self showLoadViewExt:rect backColor:backColor info:@"数据正在载入中..."];
}

-(void)showLoadViewExt:(CGRect)rect backColor:(UIColor*)backColor info:(NSString*)info
{
    if(_comLoadView != nil)
    {
        [_comLoadView removeFromSuperview];
        _comLoadView = nil;
    }
    
    _comLoadView = [[CommonLoadingView alloc] initWithInfo:rect info:info];
    _comLoadView.backgroundColor = backColor;
    _comLoadView.alpha = 1.0;
    [self.view addSubview:_comLoadView];
}

-(void)showTipInfo:(NSString*)tipInfo
{
    BOOL isExist = NO;
    for(UIView* v in self.view.subviews)
    {
        if([v isKindOfClass:[TipView class]])
        {
            TipView* tipView = (TipView*)v;
            [tipView showTipInfo:tipInfo];
            isExist = YES;
            break;
        }
    }
    if(!isExist)
    {
        TipView* tipView = [[TipView alloc] initWithTipInfo:tipInfo];
        [self.view addSubview:tipView];
    }
}

-(void)showSelectImageView
{
    UIActionSheet *myActionSheet = [[UIActionSheet alloc]
                     initWithTitle:nil
                     delegate:self
                     cancelButtonTitle:@"取消"
                     destructiveButtonTitle:nil
                     otherButtonTitles: @"打开照相机", @"从手机相册获取",nil];
    
    [myActionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    //呼出的菜单按钮点击后的响应
    if (buttonIndex == actionSheet.cancelButtonIndex)
    {
        NSLog(@"取消");
    }
    
    switch (buttonIndex)
    {
        case 0:  //打开照相机拍照
            [self takePhoto];
            break;
            
        case 1:  //打开本地相册
            [self localPhoto];
            break;
    }
}

//开始拍照
-(void)takePhoto
{
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        picker.sourceType = sourceType;
        [self presentModalViewController:picker animated:YES];
    }else
    {
        NSLog(@"模拟其中无法打开照相机,请在真机中使用");
    }
}

//打开本地相册
-(void)localPhoto
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentModalViewController:picker animated:YES];
}

//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
    
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        [snapArray addObject:image];
    }
    [picker dismissModalViewControllerAnimated:YES];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    NSLog(@"您取消了选择图片");
    [picker dismissModalViewControllerAnimated:YES];
}

-(void)showNoContentView:(UIImage *)tipicon btnicon:(UIImage *)btnicon btntitle:(NSString *)btntitle textTitle:(NSString *)textTitle tipInside:(int)inside ishidebtn:(BOOL)ishide
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, TopBarHeight+inside, ScreenWidth, ScreenHeight-TopAndSystemHeight)];
    view.tag = NOCONTENTVIEWTAG;
    view.backgroundColor = UIColorRGB(236);
    UIImage *icon = nil;
    if(tipicon == nil)
        icon = [UIImage imageNamed:@"document"];
    else
        icon = tipicon;
    UIImageView *iconview = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth-icon.size.width)/2.0, view.frame.size.height/5.0, icon.size.width, icon.size.height)];
    
    UILabel *tiplab = [self createLabel:CGRectMake(0, iconview.frame.size.height+iconview.frame.origin.y, ScreenWidth, 40) title:@"这里暂时还没有内容哦！" fontSize:13 textColor:UIColorRGB(139)];
    tiplab.textAlignment = NSTextAlignmentCenter;
    [view addSubview:tiplab];
    iconview.image = icon;
    [view addSubview:iconview];
    
    
    //调节高度
    NSString *info  = textTitle;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey:NSFontAttributeName];
    CGSize size = [info boundingRectWithSize:CGSizeMake(ScreenWidth-24, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    UITextView *tipTextView = [[UITextView alloc]initWithFrame:CGRectMake(20, tiplab.frame.origin.y + tiplab.frame.size.height-10, ScreenWidth-40, size.height+50)];
    tipTextView.backgroundColor = UIColorRGB(236);
    tipTextView.textColor = UIColorRGB(153);
    tipTextView.scrollEnabled = NO;
    tipTextView.selectable = NO;
    tipTextView.font = [UIFont systemFontOfSize:13];
    tipTextView.textAlignment = NSTextAlignmentCenter;
    tipTextView.text = textTitle;
    [view addSubview:tipTextView];
    //    UIButton *seebtn = [self createBtn:CGRectMake((ScreenWidth-150)/2.0, view.frame.size.height-view.frame.size.height*(1/5.0), 150, 40) title:btntitle iconImage:btnicon backgroundImage:[UIImage imageNamed:@"textbox"] tag:0 textColor:nil];
    UIButton *seebtn = [UIButton buttonWithType:UIButtonTypeCustom];
    seebtn.frame = CGRectMake((ScreenWidth-150)/2.0, view.frame.size.height-view.frame.size.height*(1.5/5.0), 150, 40);
    [seebtn setBackgroundImage:[UIImage imageNamed:@"textbox"] forState:UIControlStateNormal];
    [seebtn setImage:btnicon forState:UIControlStateNormal];
    [seebtn setTitle:[NSString stringWithFormat:@"  %@",btntitle] forState:UIControlStateNormal];
    seebtn.titleEdgeInsets = UIEdgeInsetsMake(-4, 0, 0, 0);
    seebtn.imageEdgeInsets = UIEdgeInsetsMake(-4, 0, 0, 0);
    seebtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [seebtn addTarget:self action:@selector(nocontentAction) forControlEvents:UIControlEventTouchUpInside];
    seebtn.hidden = ishide;
    [view addSubview:seebtn];
    
    [self.view addSubview:view];
    
    
}

-(void)closeNoContentView
{
    UIView *view = [self.view viewWithTag:NOCONTENTVIEWTAG];
    [view removeFromSuperview];
}
-(UIButton *)createBtn:(CGRect)frame title:(NSString *)title iconImage:(UIImage *)iconImage backgroundImage:(UIImage *)bgImage tag:(int)tag textColor:(UIColor *)textColor
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    btn.tag = tag;
    btn.titleLabel.font = [UIFont systemFontOfSize:frame.size.height];
    if(textColor != nil)
        [btn setTitleColor:textColor forState:UIControlStateNormal];
    if(bgImage != nil)
        [btn setBackgroundImage:bgImage forState:UIControlStateNormal];
    if(iconImage != nil)
        [btn setImage:iconImage forState:UIControlStateNormal];
    if(title != nil)
        [btn setTitle:title forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

-(UILabel *)createLabel:(CGRect)frame title:(NSString *)title fontSize:(int)fontSize textColor:(UIColor *)textColor
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.backgroundColor = [UIColor clearColor];
    label.text = title;
    if(textColor != nil)
        label.textColor = textColor;
    label.font = [UIFont systemFontOfSize:fontSize];
    return label;
}

-(void)btnAction:(id)sender
{
    
}



@end
